package com.qqtech.qquan.hi.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.hi.dao.HiRuleDao;
import com.qqtech.qquan.hi.enums.HiRuleClassifyDictEnum;
import com.qqtech.qquan.hi.model.HiRule;
import com.qqtech.qquan.hi.model.HiRuleResultVo;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.service.UserBaseService;

@Service
public class HiUserServiceImpl extends BaseServiceImpl<HiRule> implements HiUserService {
	public static final Logger log = LoggerFactory.getLogger(HiUserServiceImpl.class);

	@Resource
	private HiRuleDao hiRuleDao;

	@Override
	protected BaseDao<HiRule> getBaseDao() {
		return hiRuleDao;
	}

	@Resource
	private HiRuleService hiRuleService;
	
	@Resource
	private HiRuleResultService hiRuleResultService;
	
	@Resource
	private UserBaseService userBaseService;

	@Override
	public void register(Integer userId) {
		if (userId != null) {
			Integer classify = HiRuleClassifyDictEnum.REGISTER.getCode();
			//是否有注册送荣誉值的规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				hiRuleResultService.handleSave(userId, classify, null, 1, null);
			}
		}
	}

	@Override
	public void uploadLogo(Integer userId) {
		if (userId != null) {
			Integer classify = HiRuleClassifyDictEnum.UPLOAD_USER_LOGO.getCode();
			//是否有上传用户头像送荣誉值的规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, null);
				if (hiRuleResultNewest == null) { //首次上传头像
					hiRuleResultService.handleSave(userId, classify, null, 1, null);
				}
			}
		}
	}

	@Override
	public void editNick(Integer userId) {
		if (userId != null) {
			Integer classify = HiRuleClassifyDictEnum.UPDATE_NAME.getCode();
			//是否有修改昵称送荣誉值的规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, null);
				if (hiRuleResultNewest == null) { //首次修改昵称
					hiRuleResultService.handleSave(userId, classify, null, 1, null);
				}
			}
		}
	}

	@Override
	public void addFriend(Integer userId, Integer friendId) {
		if (userId != null && friendId != null) {
			//是否同一社区
			UserBase u = userBaseService.getById(userId);
			UserBase f = userBaseService.getById(friendId);
			boolean isSameCommunity = u.getCommunityId().intValue() == f.getCommunityId().intValue();
			if (isSameCommunity) {
				Integer classify = HiRuleClassifyDictEnum.ADD_FRIEND.getCode();
				//是否有添加好友送荣誉值的规则
				boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
				if (hasRule) {
					//奖励user
					HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, friendId);
					if (hiRuleResultNewest == null) {
						hiRuleResultService.handleSave(userId, classify, friendId, 1, hiRuleResultNewest);
					}
					//奖励friend
					hiRuleResultNewest = hiRuleResultService.getNewest(friendId, classify, userId);
					if (hiRuleResultNewest == null) {
						hiRuleResultService.handleSave(friendId, classify, userId, 1, hiRuleResultNewest);
					}
				}
			}
		}
	}



}